package com.aaa.controller;

import com.aaa.entity.Staff;
import com.aaa.service.OrderService;
import com.aaa.service.OrderdetailsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RequestMapping("order")
@RestController
public class OrderController {
    @Resource
    private OrderdetailsService orderdetailsService;
    @Resource
    private OrderService orderService;
    @RequestMapping("loadfind")
    public Object loadfind(Integer pageNum, Integer pageSize,String ordernum,String rtid,String state ) throws ParseException {
        System.out.println("state"+state);
        System.out.println("ordernum"+ordernum);
        System.out.println("rtid"+rtid);
        System.out.println("pageSize"+pageSize);
        System.out.println("pageNum"+pageNum);
        Page p=PageHelper.startPage(pageNum,pageSize,true);//将当前页和每页的数量放到PageHelper
        List<Map<String,Object>> list = orderService.loadfind(state,ordernum,rtid);//接受前台返回结果集
        PageInfo<Map<String,Object>> pageInfo=new PageInfo<>(list);//用PageInfo分页处理数据
        return pageInfo;
    }
    @RequestMapping("finddetails")
    public Object finddetails(Integer oid) throws ParseException {
        return orderdetailsService.finddetails(oid);
    }

    /**
     * 获取前台本周数据
     * @return 返回给业务层处理
     */
    @RequestMapping("week")
    public List<Map> week(){
        return orderService.week();
    }

    /**
     * 获取前台上周数据
     * @return 返回数据
     */
    @RequestMapping("lastweek")
    public List<Map> lastweek(){
        return orderService.lastweek();
    }

    /**
     * 获取前台的请求返回当月收入
     */
    @RequestMapping("month")
    public List<Map> month(){
        return orderService.month();
    }


    @RequestMapping("some")
    public List<Map> some(String begin, String finish){
        return orderService.some(begin,finish);
    }

    @RequestMapping("changemoney")
    public Object changemoney(Authentication authentication,double money,Integer oid,Integer rid) throws ParseException {
        Integer staff = ((Staff) authentication.getPrincipal()).getId();
        return orderService.changemoney(money,oid,rid,staff);
    }
    @RequestMapping("finddate")
    public List<Map<String ,Object>> finddate(Integer tid) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String ,Object>> list=orderService.finddate(tid);
        List<Map<String ,Object>> list1=new ArrayList<>();
        if(list.size()>0&&list!=null){
            for (int i = 0; i < list.size(); i++) {
                if(Integer.parseInt( list.get(i).get("num").toString())<=-1){
                    list1.add(list.get(i));
                    break;
                }
            }
        }
        System.out.println(list1);
        return list1;
    }
    @RequestMapping("checkmoney")
    public Object checkmoney(String enddate, Integer rid) throws ParseException {
        return orderService.checkmoney(enddate,rid);
    }
    @RequestMapping("checkinOrder")
    public Object checkinOrder(Authentication authentication,String enddate, Integer rid, String name, String phone, String idcard, String money){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        Integer staff = ((Staff) authentication.getPrincipal()).getId();
        String orderNo = Math.floor(((Math.random()*1000000+1000001)))+""; // 生成订单号
        orderNo=orderNo.replace(".","");
        enddate=enddate.replace("T"," ").substring(0,19);
        String startdate=sdf.format(now).replace("T"," ").substring(0,19);
        String state="已入住";
        return orderService.checkinOrder(staff,orderNo,name,phone,idcard,rid,money,startdate,enddate,state);
    }
}
